#include "TshEvent.h"
#include "InputEvent.h"
#include "InputEventCtrl.h"
#include <stdio.h>
#include <pthread.h>

void log(int type, int code, int value) {
    static int x = 0;
    static int y = 0;
    static int id = 0;
    do {
        if (type == EV_KEY) {
            printf("KEY--> type:%d, code:%d, value:%d \n", type, code, value);
            break;
        } 
        if (type == EV_REL) {
            printf("REL--> type:%d, code:%d, value:%d \n", type, code, value);
            break;
        }
        if (type == EV_ABS) 
        {
            switch(code)
            {
                case ABS_MT_POSITION_X:   /* Center X touch position */
                    x = value;
                break;
                case ABS_MT_POSITION_Y:   /* Center Y touch position */
                    y = value;
                    printf("touch(x=%d,y=%d)\n", x,y);
                break;
                case ABS_MT_TRACKING_ID:  /* Unique ID of initiated contact */
                    id = value;
                    printf("touch_id=%d\n", id);
                break;
            }
        }
    } while(0);
    fflush(stdout);
}


void* pt_callback(void * arg)
{
    InputEventCtrl *inputEventCtrl = (InputEventCtrl*)(arg);
    inputEventCtrl->run();
    return NULL;
}

int main()
{
    pthread_t ptid;
    InputEventCtrl inputEventCtrl;
    inputEventCtrl.inputEventDeal = log;
    if (inputEventCtrl.init()) {
        inputEventCtrl.addAepollFile("/dev/input/event5");
        int ptre = pthread_create(&ptid, NULL, pt_callback, (void*)(&inputEventCtrl));
        if (ptre != 0) {
            printf("inputEventCtrl thread create fail!\n");
        } 
        else {
            pthread_join(ptid, NULL);
        }
    } 
    else {
        printf("init fail!\n");
    }

    return 0;
}